import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '../components/Dashboard.vue'
import DashboardHome from '../components/DashboardHome.vue'
import DocumentList from '../components/DocumentList.vue'
import DocumentUpload from '../components/DocumentUpload.vue'
import SharedDocuments from '../components/SharedDocuments.vue'
import UserProfile from '../components/UserProfile.vue'
import SystemSettings from '../components/SystemSettings.vue'
import About from '../components/About.vue'
import Login from '../components/Login.vue'
import Notifications from '../components/Notifications.vue'
import DocumentParser from '../components/DocumentParser.vue'
import ExerciseList from '../components/ExerciseList.vue'
import ExerciseDetail from '../components/ExerciseDetail.vue'
import ExercisePlatform from '../components/ExercisePlatform.vue'
import BackgroundSettings from '../components/common/BackgroundSettings.vue'
import BackgroundSettingsTest from '../components/common/BackgroundSettingsTest.vue'
import QuestionParser from '@/components/QuestionParser.vue'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    component: Dashboard,
    children: [
      {
        path: '',
        name: 'DashboardHome',
        component: DashboardHome
      },
      {
        path: 'documents',
        name: 'Documents',
        component: DocumentList
      },
      {
        path: 'upload',
        name: 'Upload',
        component: DocumentUpload
      },
      {
        path: 'parser',
        name: 'Parser',
        component: DocumentParser
      },
      {
        path: 'shared',
        name: 'Shared',
        component: SharedDocuments
      },
      {
        path: 'exercises',
        name: 'Exercises',
        component: ExerciseList
      },
      {
        path: 'exercises/my-exercises',
        name: 'MyExercises',
        component: () => import('../components/exercise/MyExercises.vue')
      },
      {
        path: 'exercises/recent',
        name: 'RecentExercises',
        component: () => import('../components/exercise/RecentExercises.vue')
      },
      {
        path: 'exercises/statistics',
        name: 'ExerciseStatistics',
        component: () => import('../components/exercise/ExerciseStatistics.vue')
      },
      {
        path: 'exercises/create',
        name: 'CreateExerciseSet',
        component: () => import('../components/exercise/CreateExerciseSet.vue')
      },
      {
        path: 'exercises/simple-create',
        name: 'SimpleExerciseCreator',
        component: () => import('../components/exercise/SimpleExerciseCreator.vue')
      },
      {
        path: 'exercises/:id(\\d+)',
        name: 'ExerciseDetail',
        component: ExerciseDetail,
        props: true
      },
      {
        path: 'exercises/:id(\\d+)/practice',
        name: 'ExercisePlatform',
        component: ExercisePlatform,
        props: true
      },
      {
        path: 'profile',
        name: 'Profile',
        component: UserProfile
      },
      {
        path: 'settings',
        name: 'Settings',
        component: SystemSettings
      },
      {
        path: 'about',
        name: 'About',
        component: About
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: Notifications
      },
      {
        path: 'background-test',
        name: 'BackgroundTest',
        component: BackgroundSettingsTest
      },
      {
        path: 'question-parser',
        name: 'QuestionParser',
        component: QuestionParser,
        meta: {
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const publicPages = ['/login']
  const authRequired = !publicPages.includes(to.path)
  const loggedIn = localStorage.getItem('access_token')

  if (authRequired && !loggedIn) {
    return next('/login')
  }

  next()
})

export default router